//
//  BaseTextFieldCon.swift
//  Base
//
//  Created by shufang zou on 2017/9/22.
//  Copyright © 2017年 敬信. All rights reserved.
//

import UIKit
import Extend

public var kMaxNickCount  : NSInteger = 17
public var kMaxPwdCount   : NSInteger = 23
public var kMaxSecretCount: NSInteger = 8
public var kMinSecretCount: NSInteger = 6
public var kMaxEmailCount : NSInteger = 100
public var kMaxPhoneCount : NSInteger = 12
public var kMaxPhoneNumCount : NSInteger = 10
public var kMaxCodeCount  : NSInteger = 5
public var kMaxIntroCount : NSInteger = 599
public var kMaxPayCount   : NSInteger = 2

open class BaseTextFieldCon: BaseViewCon, UITextFieldDelegate {

    public func hiddenKeyBoard() {
        let chilrenviews = self.view.subviews
        for chilren in chilrenviews {
            if chilren .isKind(of: UITextField.self) {
                chilren .resignFirstResponder()
            }
        }
    }
    
    override open func viewWillDisappear(_ animated: Bool) {
        super .viewWillDisappear(true)
        self.hiddenKeyBoard()
    }
    
    override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.hiddenKeyBoard()
    }
    
    public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string.count >= 1 {
            let temp = string.components(separatedBy: NSCharacterSet.whitespaces).joined(separator: "")
            if temp.isEmpty {
                return false
            }
           
            return changeCharacter(textField, replaceString: string)
        }
        
        return true
    }
    
    
    open func changeCharacter(_ textField: UITextField,replaceString string: String) -> Bool {
        return true
    }

    
//    删除空字符串
    public func deleteEmptyC(_ string:String) -> String {
       return string.replacingOccurrences(of: " ", with: "")
    }
    
    
    public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.hiddenKeyBoard()
        return true
    }
}

